package Veterinaria.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Veterinaria.excepcion.DAOExcepcion;
import Veterinaria.modelo.Factura;
import Veterinaria.modelo.Persona;
import Veterinaria.negocio.GestionFactura;
import Veterinaria.negocio.GestionPersona;

/**
 * Servlet implementation class IngresoUsuarioServlet
 */
@WebServlet("/IngresoUsuarioServlet")
public class IngresoUsuarioServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IngresoUsuarioServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String usuario = request.getParameter("txtUsuario");
		String password = request.getParameter("txtPassword");
		
		GestionPersona persona = new GestionPersona();
		
		try {
			Persona per = new Persona();
			per = persona.obtenerUsuario(usuario, password);
			//System.out.println("Estoy Aqui 0");
			if (per == null){
				//System.out.println("Estoy Aqui 1");
				String res = "Usuario y/o password incorrectos";
				
				// 4. Enviar objetos al JSP
				request.setAttribute("RESULTADO", res);
				RequestDispatcher rd = request
						.getRequestDispatcher("ingreso.jsp");
				rd.forward(request, response);	
			}
			else{
				// Guardar en el ambiente de request
				
				request.setAttribute("usuario", per);
				String user ="Bienvenido : " + (per.getNombre_persona() + " " + per.getApellido_persona());
				
				// 4. Enviar objetos al JSP
				request.setAttribute("RESULTADO", user);
				RequestDispatcher rd = request
						 .getRequestDispatcher("menu.jsp");
				rd.forward(request, response);	
			}
			

		} catch (DAOExcepcion e) {
			System.out.println(e.getMessage());
			//RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			//rd.forward(request, response);
		}
	}

}
